const Router = require('koa-router');
const axios = require('axios');
const router = new Router();

// 推荐列表
router.get('/getDiscList', async ctx => {
    var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
    ctx.set('Content-Type', 'application/json');
    try {
        const response = await axios.get(url, {
            headers: {
                referer: 'https://c.y.qq.com/',
                host: 'c.y.qq.com'
            },
            params: ctx.request.query
        });
        ctx.body = JSON.stringify(response.data);
    } catch(err) {
        ctx.body = err;
    }
})

// 推荐歌单下的歌曲列表
router.get('/getSongList', async ctx => {
    const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
    ctx.set('Content-Type', 'application/json');
    try {
        const response = await axios.get(url, {
            headers: {
                referer: 'https://c.y.qq.com/',
                host: 'c.y.qq.com'
            },
            params: ctx.request.query
        });
        ctx.body = JSON.stringify(response.data);
    } catch(err) {
        ctx.body = err;
    }
})


// 获取当前歌手音乐详情链接
router.get('/lyric', async ctx => {
    var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg';
    try {
        const response = await axios.get(url, {
            headers: {
              referer: 'https://c.y.qq.com/',
              host: 'c.y.qq.com'
            },
            params: ctx.request.query
            // params: cpQuery
        });
        var ret = response.data;
        if (typeof ret === 'string') {
            var reg = /^\w+\(({[^()]+})\)$/
            var matches = ret.match(reg)
            if (matches) {
                ret = JSON.parse(matches[1])
            }
        }
        ctx.body = JSON.stringify(ret);
    } catch(err) {
        ctx.body = err;
    }
})

// 关键字搜索歌曲
router.get('/search', async ctx => {
    const url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
    try {
        const response = await axios.get(url, {
            headers: {
              referer: 'https://c.y.qq.com/',
              host: 'c.y.qq.com'
            },
            params: ctx.request.query
        })
        ctx.body = JSON.stringify(response.data);
    } catch(err) {
        ctx.body = err;
    }
})

module.exports = router;